import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName Day3LC1381
 * @Description 1381. 设计一个支持增量操作的栈
 * @Author clockTown
 * @Date 2021/9/12 14:39
 * @Version 1.0
 */
class Day3LC1381 {
}
/**
 * Your CustomStack object will be instantiated and called as such:
 * CustomStack obj = new CustomStack(maxSize);
 * obj.push(x);
 * int param_2 = obj.pop();
 * obj.increment(k,val);
 */

/**
 * 数组+辅助数组
 */
class Day3LC1381V1 {
    int[] stack;
    int[] add;
    int top;

    public Day3LC1381V1(int maxSize) {
        stack = new int[maxSize];
        add = new int[maxSize];
        top = -1;
    }

    public void push(int x) {
        if (top != stack.length - 1) {
            ++top;
            stack[top] = x;
        }
    }

    public int pop() {
        if (top == -1) {
            return -1;
        }
        int ret = stack[top] + add[top];
        if (top != 0) {
            add[top - 1] += add[top];
        }
        add[top] = 0;
        --top;
        return ret;
    }

    public void increment(int k, int val) {
        int limit = Math.min(k - 1, top);
        if (limit >= 0) {
            add[limit] += val;
        }
    }
}

/**
 * 数组模拟
 */
class Day3LC1381V0 {
    int[] arr;
    int size = 0;

    public Day3LC1381V0(int maxSize) {
        this.arr = new int[maxSize];
    }

    public void push(int x) {
        if (size < arr.length){
            arr[size++] = x;
        }
    }

    public int pop() {
        if (size == 0){
            return -1;
        }
        int res = arr[size - 1];
        size--;
        return res;
    }

    public void increment(int k, int val) {
        for (int i = 0; i < Math.min(size, k); i++) {
            arr[i] += val;
        }
    }
}